//
//  LGMallList.swift
//  LeGou
//
//  Created by lxm on 2022/9/21.
//

import UIKit

class LGMallList: ZMViewController, HomeHotFlowLayoutDelegate, ListRequestObjct {
    //MARK: --------------------属性--------------------
    var scrollViewDidScrollBlock: ((UIScrollView) -> ())?
    var listData = [VIPMallModels.Goods]()
    var page = 1
    var pageSize = 10
    var refreshScrollView : UIScrollView {
        return collectionView
    }
    
    
    //MARK: --------------------UI控件--------------------
    var collectionView : UICollectionView!
    //MARK: --------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
        loadList()
    }
    //MARK: --------------------UI--------------------
    func setUI() {
        title = ""
        let layout = HomeHotFlowLayout()
        layout.scrollDirection = .vertical
        layout.sectionInset = UIEdgeInsets(top: 16, left: 12, bottom: 18, right: 12)
        layout.minimumLineSpacing = 7
        layout.minimumInteritemSpacing = 7
        layout.delegate = self
        collectionView = UICollectionView(frame: CGRect(), collectionViewLayout: layout)
        collectionView.register(UINib(nibName: "LgMallListCell", bundle: nil), forCellWithReuseIdentifier: "LgMallListCell")
        view.addSubview(collectionView)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .clear
        view.backgroundColor = .clear
        collectionView.alwaysBounceVertical = true
        collectionView.showsVerticalScrollIndicator = false
        
        addRefresh(scrollView: collectionView, addHead: true, addFoot: true) { [weak self] refresh in
            guard let self = self else {return}
            if refresh {
                self.page = 1
            }
            self.loadList()
        }
    }
    func addConstraints() {
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    //MARK: --------------------methods--------------------
    func bindAction() {
        
    }
    
    func giveSize(titleStr : String) -> CGSize {
//        let imageStr = getAtrStr(titleStr: titleStr)
        let cellWidth = (SCREEN_W - 24 - 7 ) / 2
        let width = cellWidth - 24
        let f = (titleStr as NSString).boundingRect(with: CGSize(width: width, height: 300), options: .usesLineFragmentOrigin, attributes: [.font : UIFont.systemFont(ofSize: 14, weight: .medium)], context: nil)
        return f.size
    }
    
    func loadList() {
        ZMUtil.listRequest(type: VIPMallModels.Goods.self, api: LGMallAPI.LG_mall_list(["limit" : pageSize, "page" : page])) { resp in
            self.listRequest(resp)
        }
    }
}

extension LGMallList : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        collectionView.mj_footer?.isHidden = listData.count == 0
        return listData.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let data = listData[indexPath.row]
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LgMallListCell", for: indexPath) as! LgMallListCell
        cell.label.text = data.storeName
        cell.price.text = data.price.zm.toString(keep: 2)
        cell.saleCount.text = "销量:\(data.ficti+data.sales)"
        cell.img.zm.setImage(urlStr: data.image)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width : CGFloat = (SCREEN_W - 24 - 7) / 2
        let d = listData[indexPath.row]
        let size = giveSize(titleStr: d.storeName)
//        print("size is ", size)
        return CGSize(width: width, height: width + 12 + size.height + 70) // 20 是已售 //+ (isMili ? 0 : 0))
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let data = listData[indexPath.row]
        router.push("xm://main/VIPMall/goods_detail", context: ["id" : data.id])
    }
}
